
// Author:					HungNM
// Created:					16-08-2012
// Last Modified:			16-08-2012
//

using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using eProject.WebHelper;
using eProject.Data.MSSQL;
	
namespace eProject.Data
{
	
	public static class DBSeatClass
    {
        /// <summary>
        /// Gets the connection string.
        /// </summary>
        /// <returns></returns>
        private static string GetConnectionString()
        {
            return ConnectionHelper.GetConnectionString();

        }


		/// <summary>
		/// Inserts a row in the SeatClass table. Returns new integer id.
		/// </summary>
		/// <param name="seatName"> seatName </param>
		/// <param name="discription"> discription </param>
		/// <param name="createdDate"> createdDate </param>
		/// <param name="lastUpdate"> lastUpdate </param>
		/// <returns>int</returns>
		public static int Create(
			string seatName, 
			string discription, 
			DateTime createdDate, 
			DateTime lastUpdate) 
		{
			int newID = -1;
			try
			{
				using (SqlHelper db = new SqlHelper(GetConnectionString()))
				{
					newID = Convert.ToInt32(db.ExecScalarProc("mp_SeatClass_Insert",
                        "@SeatName", seatName,
                        "@Discription", discription,
                        "@CreatedDate", createdDate,
                        "@LastUpdate", lastUpdate
					));
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return newID;
		}


		/// <summary>
		/// Updates a row in the SeatClass table. Returns true if row updated.
		/// </summary>
		/// <param name="id"> id </param>
		/// <param name="seatName"> seatName </param>
		/// <param name="discription"> discription </param>
		/// <param name="createdDate"> createdDate </param>
		/// <param name="lastUpdate"> lastUpdate </param>
		/// <returns>bool</returns>
		public static bool Update(
			int  id, 
			string seatName, 
			string discription, 
			DateTime createdDate, 
			DateTime lastUpdate) 
		{
			int rowsAffected = -1;
			try
			{
				using (SqlHelper db = new SqlHelper(GetConnectionString()))
				{
					rowsAffected = db.ExecNonQueryProc("mp_SeatClass_Update",
						"@Id", id,
						"@SeatName", seatName,
						"@Discription", discription,
						"@CreatedDate", createdDate,
						"@LastUpdate", lastUpdate
					);
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return (rowsAffected > 0);
			
		}
		
		/// <summary>
		/// Deletes a row from the SeatClass table. Returns true if row deleted.
		/// </summary>
		/// <param name="id"> id </param>
		/// <returns>bool</returns>
		public static bool Delete(
			int id) 
		{
			int rowsAffected = -1;
			try
			{
				using (SqlHelper db = new SqlHelper(GetConnectionString()))
				{
					rowsAffected = Convert.ToInt32(db.ExecNonQueryProc("mp_SeatClass_Delete",
						"@Id", id
					));
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return (rowsAffected > 0);
			
		}
		
		/// <summary>
		/// Gets an IDataReader with one row from the SeatClass table.
		/// </summary>
		/// <param name="id"> id </param>
		public static IDataReader GetOne(
			int  id)  
		{
			SqlDataReader reader = null;
			try
			{
				SqlHelper db = new SqlHelper(GetConnectionString());				
				reader = db.ExecDataReaderProc("mp_SeatClass_SelectOne",
					"@Id", id
				);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return reader;
		}
		
		/// <summary>
		/// Gets a count of rows in the SeatClass table.
		/// </summary>
		public static int GetCount()
		{
			int count = 0;
			try
			{
				using (SqlHelper db = new SqlHelper(GetConnectionString()))
				{
					count = Convert.ToInt32(db.ExecScalarProc("mp_SeatClass_GetCount"));
				}
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return count;	
		}
		
		/// <summary>
		/// Gets an IDataReader with all rows in the SeatClass table.
		/// </summary>
		public static IDataReader GetAll()
		{
			SqlDataReader reader = null;
			try
			{
				SqlHelper db = new SqlHelper(GetConnectionString());				
				reader = db.ExecDataReaderProc("mp_SeatClass_SelectAll");
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return reader;
		}

        /// <summary>
        /// Gets an IDataReader with all rows in the SeatClass table.
        /// </summary>
        public static IDataReader GetAllBySchedule(long scheduleId)
        {
            SqlDataReader reader = null;
            try
            {
                SqlHelper db = new SqlHelper(GetConnectionString());
                reader = db.ExecDataReaderProc("mp_SeatClass_SelectBySchedule",
                    "@ScheduleId", scheduleId);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return reader;
        }
		
		/// <summary>
		/// Gets a page of data from the SeatClass table.
		/// </summary>
		/// <param name="pageNumber">The page number.</param>
		/// <param name="pageSize">Size of the page.</param>
		/// <param name="totalPages">total pages</param>
		public static IDataReader GetPage(
			int pageNumber, 
			int pageSize,
			out int totalPages)
		{
			totalPages = 1;
			int totalRows
				= GetCount();
				
			if (pageSize > 0) totalPages = totalRows / pageSize;
		
			if (totalRows <= pageSize)
			{
				totalPages = 1;
			}
			else
			{
				int remainder;
				Math.DivRem(totalRows, pageSize, out remainder);
				if (remainder > 0)
				{
					totalPages += 1;
				}
			}
			
			SqlDataReader reader = null;
			try
			{
				SqlHelper db = new SqlHelper(GetConnectionString());				
				reader = db.ExecDataReaderProc("mp_SeatClass_SelectPage",
					"@PageNumber", pageNumber,
					"@PageSize", pageSize
				);
			}
			catch(Exception ex)
			{
				throw ex;
			}
			
			return reader;
		}
	
	}

}


